<% add_decidim_page_title(t("profile", scope: "layouts.decidim.user_menu")) %>
<% content_for(:subtitle) { t("profile", scope: "layouts.decidim.user_menu") } %>

<%= render layout: "layouts/decidim/shared/layout_user_profile" do %>
  <% if current_user.unconfirmed_email.present? %>
    <%# NOTE: announcement cell clean_body method will purge the button %>
    <div id="email-change-pending" class="flash warning" data-announcement>
      <div class="flash__message flex-none block leading-relaxed">
        <p>
          <strong><%= t("decidim.account.email_change.title") %></strong>
        </p>
        <p>
          <%= t("decidim.account.email_change.body1", unconfirmed_email: current_user.unconfirmed_email) %>
        </p>
        <p>
          <%== t(
            "decidim.account.email_change.body2",
            resend_link: link_to(t("decidim.account.email_change.send_again"), resend_confirmation_instructions_account_path, role: :button, method: :post, remote: true),
            cancel_link: link_to(t("decidim.account.email_change.cancel"), cancel_email_change_account_path, role: :button, method: :post, remote: true)) %>
        </p>
      </div>
    </div>
  <% end %>

  <%= decidim_form_for(@account, url: account_path, method: :put, data: { controller: "account-form" }, html: { autocomplete: "on", class: "form-defaults edit_user" }) do |f| %>
    <div class="form__wrapper pt-0">
      <%= f.upload :avatar, button_class: "button button__lg button__transparent-secondary w-full" %>

      <%= form_required_explanation %>

      <%= f.text_field :name, autocomplete: "name" %>
      <%= f.text_field :nickname, autocomplete: "nickname", data: { controller: "character-counter" } %>
      <%= f.email_field :email, disabled: current_user.unconfirmed_email.present?, autocomplete: "email", data: { original: current_user.email } %>
      <%= f.url_field :personal_url, autocomplete: "url" %>
      <%= f.text_area :about, rows: 5 %>

      <%= f.collection_select(
            :locale,
            @account.organization.available_locales,
            :to_s,
            ->(locale) { locale_name(locale) },
            help_text: t("available_locales_helper", scope:"decidim.account.show")
          ) %>
      <% if @account.password.present? %>
        <%= render partial: "password_fields", locals: { form: f, user: current_user } %>
        <%= render partial: "old_password_field", locals: { form: f, show_help_text: true } %>
      <% elsif current_organization.sign_in_enabled? %>
        <div data-controller="accordion" id="accordion-password">
          <%= button_tag(
            class: "text-lg font-semibold text-secondary underline block cursor-pointer",
            id: "accordion-trigger-panel-password",
            type: "button",
            data: { controls: "panel-password" }
          ) do %>
            <%= t("change_password", scope:"decidim.account.show") %>
          <% end %>
          <div id="panel-password" class="mt-6">
            <%= render partial: "password_fields", locals: { form: f, user: current_user } %>
          </div>
        </div>

        <%= content_tag :div, id: "panel-old-password", class: @account.errors[:old_password].any? ? nil : "hidden" do %>
          <%= render partial: "old_password_field", locals: { form: f, show_help_text: true } %>
        <% end %>
      <% end %>

      <div class="form__wrapper-block flex-col-reverse md:flex-row justify-between">
        <%= f.submit t("update_account", scope:"decidim.account.show"), class: "button button__sm md:button__lg button__secondary mr-auto !ml-0" %>
      </div>
    </div>
  <% end %>
<% end %>
